//
//  SSTextEntryValidationMK2.m
//  Investigation Catalyst
//
//  Created by Tobias Haughton on Tue Jun 08 2004.
//  Copyright (c) 2004 Starline Software Ltd. All rights reserved.
//

#import "SSTextEntryValidationMK2.h"
#import "SSCatalystDocument.h"

@implementation SSTextEntryValidationMK2

+ (id)sharedTextEntryValidation {
   static SSTextEntryValidationMK2 *sharedTextEntryValidation = nil;

   if(!sharedTextEntryValidation) {
      sharedTextEntryValidation = [[SSTextEntryValidationMK2 allocWithZone:[self zone]] init];
   }
   return sharedTextEntryValidation;
}


- (id)init
{
   if (self = [super init]) {
   }
   return self;
}

- (void)addWatchedControl:(id)watched
{
   NSNotificationCenter *nc  = [NSNotificationCenter defaultCenter];

   [nc addObserver:self
          selector:@selector(editingDidBegin:)
              name:@"NSControlTextDidBeginEditingNotification"
            object:watched];
   [nc addObserver:self
          selector:@selector(textDidChange:)
              name:@"NSControlTextDidChangeNotification"
            object:watched];
}

- (void)removeWatchedControl:(id)watched
{
   NSNotificationCenter *nc  = [NSNotificationCenter defaultCenter];
   [nc removeObserver:self
                 name:@"NSControlTextDidBeginEditingNotification"
               object:watched];
   [nc removeObserver:self
                 name:@"NSControlTextDidChangeNotification"
               object:watched];
}

- (void)editingDidBegin:(NSNotification *)note
{
   stringPreEditLength = [[(NSTextView *)[[note userInfo] objectForKey:@"NSFieldEditor"] string] length]; //we need this to make sure text was entered rather than deleted
}

- (void)textDidChange:(NSNotification *)note
{
   int i = 0;
   NSString *string = [[note object] stringValue];
   NSString *subString;
   NSRange editedRange = [(NSTextView *)[[note userInfo] objectForKey:@"NSFieldEditor"] rangeForUserTextChange];
   NSMutableArray *poisonWords = [[[NSDocumentController sharedDocumentController] currentDocument] poisonWordArray];

   while(i < [poisonWords count]) {
      NSString *poisonWord = [[poisonWords objectAtIndex:i] poisonWord];
      if(editedRange.location >= ([poisonWord length] + 1)) {
         //the edit has happened far enough into the string for it to be this invalid word
         //get the sub string so that we can check
         subString = [string substringWithRange:NSMakeRange((editedRange.location - ([poisonWord length] + 1)), ([poisonWord length]) + 1)];
         if([subString caseInsensitiveCompare:[poisonWord stringByAppendingString:@" "]] == NSOrderedSame) {
            //this string is an invalid word but it may be part of another word
            if((editedRange.location - [poisonWord length] - 1) == 0 ||
               [string characterAtIndex:(editedRange.location - [poisonWord length] - 2)] == ' ') {
               //it is either the first word or a word on its own so it is invalid
               //but has the user just deleted text up to this invalid word?
               if(stringPreEditLength < [[(NSTextView *)[[note userInfo] objectForKey:@"NSFieldEditor"] string] length]) {
                  //text was added so give the warning
                  [self displayAlertWithMessage:[[poisonWords objectAtIndex:i] warningMessage]];
               }
               break;
            }
         }

      }

      i++;
   }

   stringPreEditLength = [[(NSTextView *)[[note userInfo] objectForKey:@"NSFieldEditor"] string] length]; //we need this to make sure text was entered rather than deleted
}


- (void)displayAlertWithMessage:(NSString *)message
{
   NSRunAlertPanel(NSLocalizedString(@"Alert", nil), message, @"OK", nil, nil);
}

- (void)dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
   [super dealloc];
}


@end
